<?php


namespace app\controller;


use think\facade\Validate;
use think\Request;

class Login
{
    private $toast = 'public/toast';

    public function index()
    {
        return view('index');
    }

    public function check(Request $request)
    {
        $data = $request->param();

//        错误集合
        $errors = [];

        $validate = Validate::rule([
            'name' => 'unique:auth',
        ]);

        $result = $validate->check([
            'name' => $data['name'],
            'password' => sha1($data['password'])
        ]);

        if ($result) $errors[]= '用户名或者密码错误';

//        验证码
        if (!captcha_check($data['captcha'])) {
            $errors[] = '验证码不正确~';
        }

        if (!empty($errors)) {
            return view($this->toast,[
                'infos' => $errors,
                'url_text' => '返回登录',
                'url_path' => url('/login')
            ]);
        } else {
            session('admin', $data['name']);
            return \redirect('/');
        }
    }

    public function out()
    {
        session('admin', null);
        return redirect('/login');
    }
}